﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Telerik.Web.UI;

public partial class Admin_teachers : BasePage
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!PAConfiguration.IsSuperUser(Teacher.TEACHER_ID))
    {
      Response.Redirect("error.aspx");
    }

    //if (!(Teacher.IS_ADMIN == 1) )
    //{
    //    Response.Redirect("error.aspx");
    //}



    if (!IsPostBack)
    {
      Fill();
    }
    reset_pass.Attributes.Add("name", "reset_pass");
    grant_admin.Attributes.Add("name", "grant_admin");
    revoke_admin.Attributes.Add("name", "revoke_admin");

  }

	public override string Icon
	{
		get
		{
			return "images/admin_icon.png";
		}
	}
	protected char Letter
	{
		get
		{
			if (String.IsNullOrEmpty(Request.QueryString["letter"]))
			{
				return 'A';
			}
			if (string.Compare(Request.QueryString["letter"], "all", true) == 0)
				return ' ';

			return (char)Request.QueryString["letter"][0];
		}
	}

	protected override void Fill()
	{
		DataLayer dal = new DataLayer();
		string message;
		DataTable dt = dal.FindTeachers(PAConfiguration.ConnectionString, Letter, out message);


		if (dt == null)
		{
			rgTeachers.DataSource = null;
			rgTeachers.DataBind();
			return;
		}
	

		if (!string.IsNullOrEmpty(Sorting))
		{
			dt.DefaultView.Sort = Sorting;
		}

		rgTeachers.DataSource = dt;
		rgTeachers.DataBind();


	}
	protected void lbExport_Click(object sender, ImageClickEventArgs e)
	{
		DataLayer dal = new DataLayer();
		string message = string.Empty;
		DataTable toExcel = dal.FindTeachers(PAConfiguration.ConnectionString, Letter, out message);
		PALogManager.MessageLog(message);
	
		foreach (DataColumn dc in toExcel.Columns)
		{
			foreach (GridColumn dgf in rgTeachers.Columns)
			{
				if (dc.ColumnName == dgf.SortExpression)
				{
					dc.ColumnName = dgf.HeaderText;
					break;
				}

			}
		}
    
		Manager.ExportToCSV(
			toExcel,
			this.Context,
			"Teachers.csv");
	}
	protected void gvTeachers_Sorting(object sender, GridViewSortEventArgs e)
	{
		Sort(e);
	}
	//protected void gvTeachers_RowDataBound(object sender, GridViewRowEventArgs e)
	//{
	//    if (e.Row.RowType == DataControlRowType.Header)
	//    {
	//        this.ApplySortingToHeader(e.Row.Cells);
	//    }

	//    if (e.Row.RowType != DataControlRowType.DataRow)
	//    {
	//        return;
	//    }

	//    DataRow dr = ((DataRowView)e.Row.DataItem).Row;

	//    decimal admin = (decimal)dr["IS_ADMIN"];
	//    Image im = (Image)e.Row.FindControl("Image1");
	//    im.Visible = (admin == 1);

	//}

	protected string getIds()
	{
		string result = string.Empty;

		foreach (GridDataItem gvr in rgTeachers.Items)
		{
			Telerik.Web.UI.DataKey key = rgTeachers.MasterTableView.DataKeyValues[gvr.ItemIndex];
			CheckBox cb = (CheckBox)gvr.FindControl("Checkbox1");
			if (cb.Checked)
			{
				result += key["ID"].ToString() + ",";
			}
		}

		result = result.Trim(',');
		return result;
	}

	protected void bResetPassword_Click(object sender, EventArgs e)
	{
		DataLayer dal = new DataLayer();
		string message = string.Empty;
		dal.ResetPassword(PAConfiguration.ConnectionString, getIds(), out message);

		PALogManager.MessageLog(message);
		Fill();
	}
	protected void bGrantAdmin_Click(object sender, EventArgs e)
	{
		DataLayer dal = new DataLayer();
		string message = string.Empty;
		dal.GrantAdmin(PAConfiguration.ConnectionString, getIds(), out message);

		PALogManager.MessageLog(message);
		Fill();
	}
	protected void bRevokeAdmin_Click(object sender, EventArgs e)
	{
		DataLayer dal = new DataLayer();
		string message = string.Empty;
		dal.RevokeAdmin(PAConfiguration.ConnectionString, getIds(), out message);

		PALogManager.MessageLog(message);
		Fill();
	}


	protected void rgTeachers_SortCommand(object source, Telerik.Web.UI.GridSortCommandEventArgs e)
	{
		Sort(e);
	}

	protected void rgTeachers_PageIndexChanged(object source, Telerik.Web.UI.GridPageChangedEventArgs e)
	{
		Fill();
	}
	protected void rgTeachers_ItemDataBound(object sender, GridItemEventArgs e)
	{

		if (e.Item.DataItem == null)
			return;

		DataRow dr = ((DataRowView)e.Item.DataItem).Row;

		decimal admin = (decimal)dr["IS_ADMIN"];
		Image im = (Image)e.Item.FindControl("Image1");
		if(im != null)
			im.Visible = (admin == 1);

	}
}
